import 'dart:math';

/// 字符串工具类
class TextUtil {
  /// 字符串是否为NULL
  ///
  /// [text] 判断字符串
  static bool isEmpty(String text) {
    return text == null || text.isEmpty;
  }

  /// 字符串数组比较，返回第一个不是空的字符串
  ///
  /// [texts] 字符串比较组
  static String result(List<String> texts) {
    if (texts == null || texts.isEmpty) return '';
    return texts.firstWhere((element) => !isEmpty(element), orElse: () => '');
  }

  /// 随机字符串
  ///
  /// [length] 生成长度
  String random({int length = 15}) {
    String alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
    //生成的字符串固定长度
    String left = '';
    for (var i = 0; i < length; i++) {
      left += alphabet[Random().nextInt(alphabet.length)];
    }
    return left;
  }
}
